package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.UserInfo;
import com.yhp.util.DbUtils;

/**
 * 用户信息 DAO
 *
 * @author bing.zhang06@hand-china.com 2021/7/24 11:19
 **/
public class UserInfoDao extends DbUtils {
    /**
     * 登录验证
     *
     * @param userInfo
     * @return
     */
    public UserInfo login(UserInfo userInfo) {
        Connection connection = this.getConnection();
        String sql = "select * from user_info where name = ? and password = ? and email = ?";
        List<Object> params = new ArrayList<>();
        params.add(userInfo.getName());
        params.add(userInfo.getPassword());
        params.add(userInfo.getEmail());
        ResultSet resultSet = this.query(sql, params);
        return obtainUserInfo(resultSet);
    }

    private UserInfo obtainUserInfo(ResultSet resultSet) {
        UserInfo userInfo = null;
        try {
            if (resultSet == null || !resultSet.next()) {
                return null;
            }
            String name = resultSet.getString("name");
            userInfo = new UserInfo(name, null, null, null);
            return userInfo;
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return userInfo;
    }
}
